package com.fusetech.web.flow.controller.audit;

import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fusetech.common.core.controller.BaseController;
import com.fusetech.flow.audit.domain.AuditRecord;
import com.fusetech.flow.audit.service.AuditRecordService;
import com.fusetech.flow.audit.service.dto.AuditRecordDTO;
import com.fusetech.flow.audit.service.mapstruct.AuditRecordWrapper;
import com.fusetech.os.core.query.QueryWrapperGenerator;
import com.fusetech.os.core.response.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 审核记录表Controller
 * @author Liuwq
 * @version 2023-05-09
 */

@Api(tags ="审核记录表")
@RestController
@RequestMapping(value = "/erp/audit/auditRecord")
public class AuditRecordController extends BaseController {

	@Autowired
	private AuditRecordService auditRecordService;

	@Autowired
	private AuditRecordWrapper auditRecordWrapperImpl;

	/**
	 * 审核记录表列表数据
	 */
	//@ApiLog("查询审核记录表列表数据")
	@ApiOperation(value = "查询审核记录表列表数据")
	//@PreAuthorize({"erp:audit:auditRecord:list"})
	@GetMapping("list")
	public ResponseEntity<?> list(AuditRecordDTO auditRecordDTO, Page<AuditRecordDTO> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (auditRecordDTO, AuditRecordDTO.class);
		//queryWrapper.eq("a.auditor", getUserId());//列表权限，配置节点的对应授权人可查看
		//IPage<AuditRecordDTO> result = auditRecordService.findPage (page, queryWrapper);
		startPage();
		List<AuditRecordDTO> result = auditRecordService.findList(page,queryWrapper);

		//IPage<AuditRecordDTO> result = auditRecordService.findList (page, queryWrapper);

		return ResponseMessage.OK(result);
	}


	/**
	 * 根据Id获取审核记录表数据
	 */
	//@ApiLog("根据Id获取审核记录表数据")
	@ApiOperation(value = "根据Id获取审核记录表数据")
	//@PreAuthorize({"erp:audit:auditRecord:view","erp:audit:auditRecord:add","erp:audit:auditRecord:edit"})
	@GetMapping("queryById")
	public ResponseEntity<?> queryById(String id) {
		return ResponseMessage.OK( auditRecordService.findById ( id ) );
	}

	/**
	 * 保存审核记录表
	 */
	//@ApiLog("保存审核记录表")
	@ApiOperation(value = "保存审核记录表")
	//@PreAuthorize({"erp:audit:auditRecord:add","erp:audit:auditRecord:edit"})
	@PostMapping("save")
	public ResponseEntity<?> save(@Valid @RequestBody AuditRecordDTO auditRecordDTO) {
	 AuditRecord auditRecord=auditRecordWrapperImpl.toEntity (auditRecordDTO);
		//新增或编辑表单保存
		if(auditRecordService.saveOrUpdate(auditRecord)){
		    return ResponseMessage.OK(auditRecordWrapperImpl.toDTO(auditRecord));
		}{
		    return ResponseMessage.ERROR( "保存审核记录表失败" );   
		}
	}


	/**
	 * 删除审核记录表
	 */
	//@ApiLog("删除审核记录表")
	@ApiOperation(value = "删除审核记录表")
	//@PreAuthorize({"erp:audit:auditRecord:del"})
	@DeleteMapping("delete")
	public ResponseEntity<?> delete(String ids) {
		String idArray[] = ids.split(",");
        auditRecordService.removeByIds ( ListUtils.newArrayList ( idArray ) );
		return ResponseMessage.OK( "删除审核记录表成功" );
	}
	
	/**
     * 导出审核记录表数据
     *
     * @param auditRecordDTO
     * @param page
     * @param response
     * @throws Exception
     */
    //@ApiLog("导出审核记录表数据")
    //@PreAuthorize({"erp:audit:auditRecord:export"})
    /*@GetMapping("export")
    public void exportFile(AuditRecordDTO auditRecordDTO, Page <AuditRecordDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (auditRecordDTO, AuditRecordDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据
            
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "a.id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List<AuditRecordDTO> result = auditRecordService.findPage ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( auditRecordService, auditRecordWrapperImpl ).exportExcel ( result,  options.getSheetName ( ), AuditRecordDTO.class, fileName,options.getExportFields (), response );
    }*/

    /**
     * 导入审核记录表数据
     *
     * @return
     */
    //@PreAuthorize({"erp:audit:auditRecord:import"})
    /*@PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( auditRecordService, auditRecordWrapperImpl ).importExcel ( file, AuditRecordDTO.class );
        return ResponseMessage.OK(result);
    }*/

    /**
     * 下载导入审核记录表数据模板
     *
     * @param response
     * @return
     */
    //@PreAuthorize({"erp:audit:auditRecord:import"})
    /*@GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "审核记录表数据导入模板.xlsx";
        List<AuditRecordDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( auditRecordService, auditRecordWrapperImpl ).exportExcel ( list,  "审核记录表数据", AuditRecordDTO.class, fileName, null, response );
    }*/

}
